package lib.popup

import android.content.Context
import android.text.TextUtils
import android.text.method.LinkMovementMethod
import android.view.LayoutInflater
import androidx.databinding.DataBindingUtil
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.BasePopupView
import com.lxj.xpopup.core.CenterPopupView
import com.lxj.xpopup.enums.PopupAnimation
import com.lxj.xpopup.interfaces.SimpleCallback
import lib.popup.databinding.MviPopupConfirmBinding

/**
 * author: luoXiong
 * e-mail: 382060748@qq.com
 * date: 2020/10/27 16:58
 * version: 1.0
 * desc:
 */
class _ConfirmPopupView(context: Context) : CenterPopupView(context) {

    var mTitle: String? = null
    var mContent: String? = null
    var mContentGravity: Int? = null
    var mContentColorInt: Int? = null

    var mLeftBtnText: String? = null
    var mLeftBtnColorInt: Int? = null
    var mLeftBtnClickListener: (() -> Unit)? = null

    var mRightBtnText: String? = null
    var mRightBtnColorInt: Int? = null
    var mRightBtnClickListener: (() -> Unit)? = null
    var mOnDismissListener: (() -> Unit)? = null

    private var binding: MviPopupConfirmBinding? = null

    fun show(info: PopupCommonInfo) {
        if (TextUtils.isEmpty(mLeftBtnText)) {
            mLeftBtnText = "取消"
        }
        if (TextUtils.isEmpty(mRightBtnText)) {
            mRightBtnText = "确定"
        }
        var pwDismissCb: SimpleCallback? = null
        if (mOnDismissListener != null) {
            pwDismissCb = object : SimpleCallback() {
                override fun onDismiss(popupView: BasePopupView) {
                    mOnDismissListener!!.invoke()
                }

                // 如果你自己想拦截返回按键事件，则重写这个方法，返回true即可
                override fun onBackPressed(popupView: BasePopupView): Boolean {
                    return true
                }
            }
        }
        val builder = XPopup.Builder(context)
            .isDestroyOnDismiss(info.isDestroyOnDismiss)
            .dismissOnTouchOutside(info.isDismissOnTouchOutside)
            .dismissOnBackPressed(info.isDismissOnBackPressed)
            .popupAnimation(PopupAnimation.NoAnimation)
            .setPopupCallback(pwDismissCb)
        builder.asCustom(this).show()
    }

    override fun addInnerContent() {
        binding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout._mvi_popup_confirm, centerPopupContainer, true)
        val tvTitle = binding!!.tvTitle
        val tvContent = binding!!.tvContent
        val btnLeft = binding!!.btnLeft
        val btnRight = binding!!.btnRight
        tvTitle.text = mTitle
        // 设置内容可以滑动
        tvContent.setMovementMethod(LinkMovementMethod.getInstance())
        tvContent.text = mContent
        if (mContentColorInt != null && mContentColorInt != 0) {
            tvContent.setTextColor(mContentColorInt!!)
        }
        if (mContentGravity != null && mContentGravity!! > 0) {
            tvContent.gravity = mContentGravity!!
        }
        // 左侧按钮
        btnLeft.text = mLeftBtnText.orEmpty()
        if (mLeftBtnColorInt != null && mLeftBtnColorInt != 0) {
            btnLeft.setTextColor(mLeftBtnColorInt!!)
        }
        btnLeft.setOnClickListener {
            dismiss()
            mLeftBtnClickListener?.invoke()
        }
        // 右侧按钮
        btnRight.text = mRightBtnText.orEmpty()
        if (mRightBtnColorInt != null && mRightBtnColorInt != 0) {
            btnLeft.setTextColor(mRightBtnColorInt!!)
        }
        btnRight.setOnClickListener {
            dismiss()
            mRightBtnClickListener?.invoke()
        }
    }

    override fun getPopupHeight(): Int {
        return super.getPopupHeight()
    }

    override fun getMaxHeight(): Int {
        return super.getMaxHeight()
        //  return (ScreenUtils.getScreenHeight() * 0.85).toInt()  // 加上后,如果内容高度有5行,有时候也只显示4行,剩下的要滑动,体验不好
    }

}